﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.UI.HtmlControls;

namespace NJL.WebUsers
{
    public partial class Login : BasePageVisitor
    {
        NJL.BLL.NJL_USER bllUser = new BLL.NJL_USER();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (UserId != "")
            {
                Response.Redirect("/",true);
            }
            if (!IsPostBack)
            {
                LoadPageSEO();
            }
        }

        /// <summary>
        /// 加载页面SEO
        /// </summary>
        private void LoadPageSEO()
        {
            Page.Title = "用户登录-" + WEBSITE_SYS_BASE.WEBSITE_NAME;

            //HtmlMeta myMeta = new HtmlMeta();
            //myMeta.Name = "Keywords";
            //myMeta.Content = WEBSITE_SYS_BASE.WEBSITE_KEYWORDS;
            //this.Header.Controls.Add(myMeta);

            //HtmlMeta myMeta1 = new HtmlMeta();
            //myMeta1.Name = "Description";
            //myMeta1.Content = WEBSITE_SYS_BASE.WEBSITE_DESCRIPTION;
            //this.Header.Controls.Add(myMeta1);
        }

        protected void btnEmail_Click(object sender, EventArgs e)
        {
            string userName = txtEmail.Text;
            string pwd = hidEmail.Value;
            DataSet ds = bllUser.GetUserLoginByEmail(userName, pwd);
            if (ds.Tables[0].Rows.Count > 0)
            {
                Session.Add("userid", ds.Tables[0].Rows[0]["ID"]);
                Session.Add("username", ds.Tables[0].Rows[0]["USER_NAME"]);
                Session.Add("usertype", ds.Tables[0].Rows[0]["TYPE_CODE"]);
                string userType = ds.Tables[0].Rows[0]["TYPE_CODE"].ToString();
                //如果未激活先到激活页面
                if (userType == "-1")
                {
                    Response.Redirect("~/RegisterActive.aspx");
                }
                else if(Request.QueryString["url"]==null)
                {
                    Response.Redirect("~/");
                }
                else if (Request.QueryString["url"] != null)
                {
                    Response.Redirect(Request.QueryString["url"]);
                }
            }
            else
            {           
                Royalsoft.Common.MessageBox.Show(this, "请输入正确的账号或密码");
            }
        }
    }
}